<?php
/**
 * Created by PhpStorm.
 * User: USER
 * Date: 2017/4/24
 * Time: 16:04
 */

namespace JsonRpc\Services\Product;


use JsonRpc\Models\PCate;
use JsonRpc\Services\CommonService;
use JsonRpc\Services\System\UserService;
use JsonRpc\Utils\Utils;

class CateService
{
    use CommonService;

    private static function getBaseModel(){
        return "JsonRpc\\Models\\PCate";
    }

    //分类列表
    public static function getCateList($params)
    {

        $res = self::getInfoList($params);
        $data = Utils::make_treeTable($res,'id','parent_id','children',0);

        return $data;
    }

    //分类树获取
    public static function getCateTree($params)
    {
        $params['select'] = ['id','name as text','parent_id'];
        $res = self::getInfoList($params);
        $data = Utils::make_treeTable($res,'id','parent_id','children',0);

        return $data;
    }

    //获取单条组织信息
    public static function getCateOne($params)
    {
        $info = PCate::find($params['id']);
        if($info){
            return $info->toArray();
        }
        return [];
    }

    //分类更新
    public function saveCateInfo($params)
    {
        if(isset($params['id']) && $params['id']){
            //更新
            $model = PCate::find($params['id']);
            $model->updated_by = $params['curr_user_id'];
        }else{
            $user = UserService::getInfoById(['id'=>$params['curr_user_id']]);
            //新增
            unset($params['id']);
            $model = new PCate();
            $model->client_id = $user['client_id'];
            $model->created_by = $user['id'];
            $model->updated_by = $user['id'];
        }
        $model->parent_id = $params['parent_id'];
        $model->name = $params['name'];

        return $model->save();

    }
}